import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layouts/Layout.vue'
import { hasModulePermission } from '../utils/permission'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '数据概览' }
      },
      {
        path: 'student-review',
        name: 'StudentReview',
        component: () => import('../views/StudentReview.vue'),
        meta: { title: '留学生审核' }
      },
      {
        path: 'company-review',
        name: 'CompanyReview',
        component: () => import('../views/CompanyReview.vue'),
        meta: { title: '企业认证审核' }
      },
      {
        path: 'demand-review',
        name: 'DemandReview',
        component: () => import('../views/DemandReview.vue'),
        meta: { title: '需求内容审核' }
      },
      {
        path: 'request',
        name: 'Request',
        component: () => import('../views/Request.vue'),
        meta: { title: '企业联系请求' }
      },
      {
        path: 'student-request',
        name: 'StudentRequest',
        component: () => import('../views/StudentRequest.vue'),
        meta: { title: '学生对接请求' }
      },
      {
        path: 'student-management',
        name: 'StudentManagement',
        component: () => import('../views/StudentManagement.vue'),
        meta: { title: '学生管理' }
      },
      {
        path: 'company-management',
        name: 'CompanyManagement',
        component: () => import('../views/CompanyManagement.vue'),
        meta: { title: '企业管理' }
      },
      {
        path: 'announcement',
        name: 'Announcement',
        component: () => import('../views/Announcement.vue'),
        meta: { title: '公告管理' }
      },
      {
        path: 'admin-management',
        name: 'AdminManagement',
        component: () => import('../views/AdminManagement.vue'),
        meta: { title: '管理员管理' }
      },
      {
        path: 'operation-logs',
        name: 'OperationLogs',
        component: () => import('../views/AdminLogs.vue'),
        meta: { title: '操作日志' }
      },
      {
        path: 'admin-roles',
        name: 'AdminRoles',
        component: () => import('../views/AdminRoles.vue'),
        meta: { title: '角色管理' }
      },
      {
        path: 'banner-management',
        name: 'BannerManagement',
        component: () => import('../views/BannerManagement.vue'),
        meta: { title: '轮播图管理' }
      },
      {
        path: 'user-agreement',
        name: 'UserAgreement',
        component: () => import('../views/UserAgreement.vue'),
        meta: { title: '用户协议和隐私政策' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 检查登录状态
  if (to.path !== '/login' && !token) {
    next('/login')
    return
  }
  
  // 检查模块权限
  if (to.path !== '/login' && to.path !== '/dashboard' && to.path !== '/operation-logs' && to.path !== '/user-agreement') {
    if (!hasModulePermission(to.path)) {
      next('/dashboard')
      return
    }
  }
  
  next()
})

export default router 